/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

public final class IO {

	//~ Constructors ///////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	protected IO() { }

	//~ Methods ////////////////////////////////////////////////////////////////////////////////////
	//**********************************************************************************************
	/** Pauses the application until a Key Event is caught (E.g.: Enter key) */
	public void pause() {

		try {
			System.in.read();
		}
		catch(IOException ex) {Application.getLogger().warn(
			"Cannot pause the application: " + ex.getMessage()
		);}

	}
	//**********************************************************************************************
	/** Pauses the application until a Key Event is caught (E.g.: Enter key) */
	public void pause(String message) {

		try {
			System.out.println(message);
			System.in.read();
		}
		catch(IOException ex) {Application.getLogger().warn(
			"Cannot pause the application: " + ex.getMessage()
		);}

	}
	//**********************************************************************************************
	public File createDirectory(String path) {

		File file = new File( path );
		if (!file.exists()) file.mkdir();
		return file;

	}
	//**********************************************************************************************
	public final File createTempFile(String name) throws IOException {

		File tmp = File.createTempFile(
				name,                                  // file name prefix
				null,                                  // file name suffix. Leave it null to allow JVM to create random suffixes.
				Application.paths.temporary.getFile()  // Temporary directory to store temporary files
		);
		tmp.deleteOnExit();
		return tmp;

	}
	//**********************************************************************************************
	public final File createTempFile(String name,String type) throws IOException {

		File tmp = File.createTempFile(
				name,                                  // file name prefix
				type,                                  // file name suffix.
				Application.paths.temporary.getFile()  // Temporary directory to store temporary files
		);
		tmp.deleteOnExit();
		return tmp;

	}
	//**********************************************************************************************
	public String readString(InputStream input,String encoding,int length) throws IOException {

		try {
			// Auxiliary
			StringBuffer output = new StringBuffer(length>0 ? length : 256);
			int          data   = -1;

			// The string must be read based on its character encoding
			Reader reader = new BufferedReader(
					(encoding==null)
					? new InputStreamReader(input)
					: new InputStreamReader(input,encoding)
			);
			// Read by character, not by string since it may be an OctetSstream
			while ((data = reader.read()) != -1) {
				output.append((char)data);
			}
			return output.toString();
		}
		catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

	}
	//**********************************************************************************************
	public byte[] readBytes(InputStream input,int length) throws IOException {

		// Auxiliary
		ByteArrayOutputStream output = new ByteArrayOutputStream(length>0 ? length : 256);
		int                   data   = -1;

		// Buff the stream for faster retrieval
		input = new BufferedInputStream(input);
		while ((data = input.read()) != -1) {
			output.write(data);
		}
		return output.toByteArray();

	}
	//**********************************************************************************************
	public void readBytes(InputStream input,OutputStream output) throws IOException {

		// Auxiliary Variables
		byte[] buffer = new byte[2048];
		int    length = 0;

		// Buff the streams for faster retrieval
		input  = new BufferedInputStream(input);
		output = new BufferedOutputStream(output);
	    while ( (length = input.read(buffer) ) != -1) {
	    	output.write(buffer, 0, length);
	    };
	    output.flush();

	}
	//**********************************************************************************************

}
